<?php


namespace app\api\model;


use think\Model;

class OrderItem extends Model
{

    /**
     * 关联商品表
     *
     */
    public function goods()
    {
        return $this->hasOne('app\api\model\Goods', 'id', 'goods_id')->setEagerlyType(0);
    }

    /**
     * 关联商品规格表
     *
     */
    public function goodsspecs()
    {
        return $this->hasOne('app\api\model\GoodsSpecs', 'id', 'goods_sku_id')->setEagerlyType(0);
    }

    /**
     * 单个商品库存检测
     *
     */
    public function getStock($goods_sku_id)
    {
        $res = $this->with('goodsspecs')->where('goods_sku_id', $goods_sku_id)->find();
        if ($res['goodsspecs']['pro_stock'] > 0) {
            return $res;
        } else {
            return '库存不足';
        }
    }

    /**
     * 下单
     *
     */
    public function addOrder($user_id, $goods_id)
    {

    }

    /**
     * 将商品加入到订单商品表
     *
     */
    public function joinOrderItem($data)
    {
        $res = $this->insert($data);
        return $res;
    }
}